<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ name: 'home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="content">
      <!-- 头部功能 -->
      <template #header>
        <div>
          <el-button type="primary" size="medium">添加角色</el-button>
        </div>
      </template>
      <!-- 正文内容 -->
      <el-table
        :data="roles"
        style="width: 100%"
        border
        size="small"
        v-loading="loading"
      >
        <template #empty>
          <el-empty description="暂无数据"></el-empty>
        </template>
        <el-table-column type="expand" label="明细" width="60px" #default="{row}">
          <el-empty v-if="!row.children||row.children.length==0" :image-size="40" description="未分配权限"></el-empty>
          <!-- 参考代码 -->
          <ul class="role-list role-box">
            <li class="role-item" v-for="itemA in row.children" :key="itemA.id">
              <div class="role-tag">
                <el-tag effect="dark" closable>{{ itemA.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </div>
              <ul class="role-list">
                <li class="role-item" v-for="itemB in itemA.children" :key="itemB.id">
                  <div class="role-tag">
                    <el-tag type="success" effect="dark" closable>{{ itemB.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </div>
                  <ul class="role-list">
                    <li class="role-item" style="flex-wrap: wrap;">
                      <div class="role-tag" v-for="itemC in itemB.children" :key="itemC.id">
                        <el-tag type="warning" effect="dark" closable>{{ itemC.authName }}</el-tag>
                      </div>
                    </li>
                  </ul>
                </li>
              </ul>
            </li>
          </ul>
          <!-- 参考代码 -->
        </el-table-column>
        <el-table-column type="index" label="序号" width="60px">
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
        <el-table-column label="操作" width="300px" #default="{ row }">
          <el-button type="primary" size="mini" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeRolesById(row.id)"
            >删除</el-button
          >
          <el-button type="warning" size="mini" icon="el-icon-setting"
            >分配权限</el-button
          >
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      query: "",
      roles: [],
    };
  },
  methods: {
    async loadRolesListPage() {
      try {
        this.loading = true;
        let result = await this.$api.getRolesList();
        if (result == "error") return;
        this.roles = result;
      } catch (error) {
        console.log("RolesView-loadRolesListPage-error:", error);
      } finally {
        this.loading = false;
      }
    },
    async removeRolesById(id) {
      try {
        console.log(id);
        // if (result == "error") return;
        // this.$message.success("删除成功");
        // let lastFlag = this.total%this.pagesize==1;
        // let lastPageFlag = Math.ceil(this.total/this.pagesize)==this.pagenum;
        // if(lastFlag&&lastPageFlag&&this.pagenum!=1){
        //   this.pagenum = this.pagenum - 1;
        // }
        // this.loadRolesListPage();
      } catch (error) {
        console.log("RolesView-removeRolesById-error:", error);
      }
    },
  },
  created() {
    this.loadRolesListPage();
  },
};
</script>


<style lang="less" scoped>
.content,
.el-pagination {
  margin-top: 20px;
}
.search-input {
  width: 300px;
}
.add-btn {
  margin-left: 20px;
}
.tip {
  font-size: 18px;
}
.tip.el-icon-error {
  color: red;
}
.tip.el-icon-success {
  color: green;
}
.role-list{
  list-style: none;
  padding: 0px;
  margin: 0px;
  .el-tag{
    margin: 10px 4px;
  }
}
.role-item{
  width: 100%;
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
  border-bottom: 1px solid #dedede;
  &:last-child{
    border-bottom: none;
  }
}
.role-item .role-tag{
  flex-shrink: 0;
  margin-right: 20px;
}
.role-item .role-list{
  flex-grow: 1;
}
.role-box{
  border-top: 1px solid #dedede;
  border-bottom: 1px solid #dedede;
  margin: 20px;
}

</style>